import global from './global.js';

let ad = null;
// 激励视频广告
let adUnitIdDouyin = "miehx4o3e5orztyhe8";
let adUnitIdKuaishou = 100070094;
let adUnitIdBaidu = "10492914";
let appUnitIdBaidu = "b8e803d2";
// 首页底部广告
let adUnitIdDouyinHomeBottom = "vknrhr8dyaxvsew357";
let adUnitIdKuaishouHomeBottom = 100070095;
// 答题底部广告
let adUnitIdDouyinAnswerBottom = "vknrhr8dyaxvsew357";
// 答题完成底部广告
let adUnitIdDouyinFinishBottom = "vknrhr8dyaxvsew357";
// 结果页底部广告
let adUnitIdDouyinResultBottom = "vknrhr8dyaxvsew357";
let adUnitIdKuaishouResultBottom = 100070095;

let provider = global.getProvider();

// 创建视频广告
function adVideoCreate() {
	console.log(provider);
	switch (provider) {
		case "douyin": {
			ad = tt.createRewardedVideoAd({
				adUnitId: adUnitIdDouyin,
			});
			break;
		}
		case "toutiao": {
			ad = tt.createRewardedVideoAd({
				adUnitId: adUnitIdDouyin,
			});
			break;
		}
		case "kuaishou": {
			ad = ks.createRewardedVideoAd({
				type: 100011056,
				unitId: adUnitIdKuaishou,
			})
			break;
		}
		case "baidu": {
			console.log(swan.createRewardedVideoAd);
			if (swan.createRewardedVideoAd) {
				ad = swan.createRewardedVideoAd({
					adUnitId: adUnitIdBaidu,
					appSid: appUnitIdBaidu
				})
			}
			break;
		}
	}
}

// 展示视频广告
function adVideoShow(callBack) {
	switch (provider) {
		case "douyin": {
			ad.load();
			ad.onClose((data) => {
				console.log("onClose", data);
				tt.hideLoading();
				if (data.isEnded) {
					callBack();
				} else {
					global.toast("观看完视频才能继续喔~");
				}
			});
			ad.onError((err) => {
				console.log("onError", err);
				tt.hideLoading();
				switch (err.errCode) {
					default: {
						callBack();
					}
				}
			});
			ad.show();
			break;
		}
		case "toutiao": {
			ad.load();
			ad.onClose((data) => {
				console.log("onClose", data);
				tt.hideLoading();
				if (data.isEnded) {
					callBack();
				} else {
					global.toast("观看完视频才能继续喔~");
				}
			});
			ad.onError((err) => {
				console.log("onError", err);
				tt.hideLoading();
				switch (err.errCode) {
					default: {
						callBack();
					}
				}
			});
			ad.show();
			break;
		}
		case "kuaishou": {
			ad.load();
			ad.onClose((data) => {
				console.log("onClose", data);
				if (data.isEnded) {
					callBack();
				} else {
					global.toast("观看完视频才能继续喔~");
				}
			});
			ad.onError((err) => {
				console.log("onError", err);
				switch (err.errCode) {
					default: {
						callBack();
					}
				}
			});
			ad.show();
			break;
		}
		case "baidu": {
			console.log(ad);
			if (ad) {
				ad.onClose((data) => {
					console.log("onClose", data);
					if (data.isEnded) {
						callBack();
					} else {
						global.toast("观看完视频才能继续喔~");
					}
				});
				ad.onError((err) => {
					console.log("err", err);
					switch (err.errCode) {
						default: {
							callBack();
						}
					}
				});
				ad.show().catch((res1) => {
					console.log("res1", res1);
					ad.load().then(res2 => {
						console.log("res2", res2);
						ad.show();
					})
				})
			} else {
				callBack();
			}
			break;
		}
		default: {
			callBack();
		}
	}
}

// 关闭视频广告
function adVideoUnload() {
	switch (provider) {
		case "baidu": {
			if (ad) {
				ad.offClose();
			}
			break;
		}
		default: {
			ad = null;
		}
	}
}

// 广告确认弹窗
function showAdConfirmModal(callBack) {
	uni.showModal({
		title: "提示",
		content: "是否看广告获取更多信息?",
		showCancel: true,
		success: (res) => {
			if (res.confirm) {
				callBack();
			}
		},
	});
}

module.exports = {
	adVideoCreate,
	adVideoShow,
	adVideoUnload,
	showAdConfirmModal,
	adUnitIdDouyinHomeBottom,
	adUnitIdDouyinAnswerBottom,
	adUnitIdDouyinFinishBottom,
	adUnitIdDouyinResultBottom,
	adUnitIdKuaishouHomeBottom,
	adUnitIdKuaishouResultBottom,
}